package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements INewsDao {

    private void clearNewsRedis(){
        Jedis jedis = new Jedis();
        jedis.del("news_id");
        jedis.del("news_title");
        jedis.del("news_content");
        jedis.del("news_type");
//        jedis.del("news_create_time");
//        jedis.del("news_update_time");
//        jedis.del("news_enable");
//        jedis.del("news_note");
    }

    private NewsModel getResult(ResultSet rs) throws SQLException {
        NewsModel news = new NewsModel();
        news.setId(rs.getInt("id"));
        news.setTitle(rs.getString("title"));
        news.setContent(rs.getString("content"));
        news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
        news.setCreateTime(rs.getString("create_time"));
        news.setUpdateTime(rs.getString("update_time"));
        news.setEnabled(rs.getInt("enable"));
        news.setNote(rs.getString("note"));
        return news;
    }
    private NewsModel getNewsResult(String sql){
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = getResult(rs);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return news;
    }
    private List<NewsModel> getNewsListResult(String sql){
        List<NewsModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                NewsModel news = getResult(rs);
                list.add(news);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public int addNews(NewsModel newsModel) {
        String sql = "insert into news (title,content,create_time,news_type) values('"+ newsModel.getTitle() +
                "','"+ newsModel.getContent() +"','"+ newsModel.getCreateTime() +
                "',"+ newsModel.getNewsType().getId() +")";
        clearNewsRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNews(int id) {
        String sql = "delete from news where id = "+id;
        clearNewsRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNews(NewsModel newsModel) {
        String sql = "update news set title='"+newsModel.getTitle() + "',content='"+ newsModel.getContent() +
                "',news_type="+ newsModel.getNewsType().getId() +"',update_time="+ newsModel.getUpdateTime() +
                " where id="+newsModel.getId();
        clearNewsRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsModel> getNewsList() {
        String sql = "select * from news";
        return getNewsListResult(sql);
    }

    @Override
    public NewsModel getNews(int id) {
        String sql = "select * from news where id="+id;
        return getNewsResult(sql);
    }

    @Override
    public List<NewsModel> getNewsList(int page, int size) {
        String sql = "select * from news limit "+ (page-1)*size +","+size;
        return getNewsListResult(sql);
    }

    @Override
    public List<NewsModel> getNewsList(int page, int size, String sort) {
        String sql = "select * from news order by "+ sort +" limit "+ (page-1)*size +","+size;
        return getNewsListResult(sql);
    }

    @Override
    public List<NewsModel> searchNews(String sql) {
        return getNewsListResult(sql);
    }

    @Override
    public long getNewsCount() {
        String sql = "select count(*) from news";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int updateEnable(int id, int enable) {
        String sql = "update news set enable="+enable+" where id="+id;
        clearNewsRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsModel> getNewsListByRedis() {
        List<NewsModel> newsList = new ArrayList<>();
        Jedis jedis = new Jedis();
        List<String> newsId = jedis.lrange("news_id",0,-1);
        for(int i=0;i<newsId.size();i++){
            NewsModel newsModel = new NewsModel();
            newsModel.setId(Integer.valueOf(newsId.get(i)));
            newsModel.setTitle(jedis.lindex("news_title",i));
            newsModel.setContent(jedis.lindex("news_content",i));
            newsModel.setNewsType(new NewsTypeDaoImpl().getNewsType(Integer.parseInt(jedis.lindex("news_type",i))));
//            newsModel.setCreateTime(jedis.lindex("news_create_time",i));
//            newsModel.setUpdateTime(jedis.lindex("news_update_time",i));
//            newsModel.setEnabled(Integer.valueOf(jedis.lindex("news_enable",i)));
//            newsModel.setNote(jedis.lindex("news_note",i));
            newsList.add(newsModel);
        }
        return newsList;
    }

    @Override
    public void setNewsListInRedis(List<NewsModel> newsList) {
        Jedis jedis = new Jedis();
        for (NewsModel newsModel:newsList) {
            jedis.rpush("news_id", String.valueOf(newsModel.getId()));
            jedis.rpush("news_title",newsModel.getTitle());
            jedis.rpush("news_content",newsModel.getContent());
            jedis.rpush("news_type", String.valueOf(newsModel.getNewsType().getId()));
//            jedis.rpush("news_create_time",newsModel.getCreateTime());
//            jedis.rpush("news_update_time",newsModel.getUpdateTime());
//            jedis.rpush("news_enable", String.valueOf(newsModel.getEnabled()));
//            jedis.rpush("news_note",newsModel.getNote());
        }
    }
}
